<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">{{$t('loginNavTitle')}}</a>
        <a class="site-navbar__brand-mini" href="javascript:;">{{$t('loginNavFoldTitle')}}</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu
        class="site-navbar__menu"
        mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal">
        <!--<el-menu-item index="1" @click="$router.push({ name: 'theme' })">
          <template slot="title">
            <el-badge value="new">
              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>-->
        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
             {{currentLanguage}}<i class="el-icon-arrow-down" style="font-size: 14px;font-weight: bold"/>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="changeLan('zh')">{{$t('language.zh')}}</el-dropdown-item>
              <el-dropdown-item @click.native="changeLan('en')">{{$t('language.en')}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img :src="userIcon" :alt="userName">{{ userName }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">{{$t('mainNavBarUpdatePwd')}}</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">{{$t('mainNavBarLogout')}}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePasswordVisible" ref="updatePassword"/>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import { langStorage, clearLoginInfo } from '@/utils'
  export default {
    data () {
      return {
        updatePasswordVisible: false,
        lan: langStorage(),
        currentLanguage: ''
      }
    },
    created () {
      let key = 'language.' + this.lan
      this.currentLanguage = this.$t(key)
    },
    components: {
      UpdatePassword
    },
    computed: {
      navbarLayoutType: {
        get () { return this.$store.state.common.navbarLayoutType }
      },
      sidebarFold: {
        get () { return this.$store.state.common.sidebarFold },
        set (val) { this.$store.commit('common/updateSidebarFold', val) }
      },
      mainTabs: {
        get () { return this.$store.state.common.mainTabs },
        set (val) { this.$store.commit('common/updateMainTabs', val) }
      },
      userName: {
        get () { return this.$store.state.user.name }
      },
      userIcon: {
        get () { return this.$store.state.user.icon }
      }
    },
    methods: {
      // 修改密码
      updatePasswordHandle () {
        this.updatePasswordVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassword.init()
        })
      },
      // 切换语言
      changeLan (lan) {
        this.$i18n.locale = lan
        langStorage(lan)
        window.location.reload()
      },
      // 退出
      logoutHandle () {
        this.$confirm(this.$t('mainNavBarLogoutConfirm'), this.$t('commonTips'), {
          confirmButtonText: this.$t('commonConfirm'),
          cancelButtonText: this.$t('commonCancel'),
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/logout'),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.code === 0) {
              clearLoginInfo()
              this.$router.push({ name: 'login' })
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
